//package com.btex.app.pager;
//
//import android.app.Dialog;
//import android.content.Intent;
//import android.net.Uri;
//import android.os.Bundle;
//import android.os.CountDownTimer;
//import android.os.Environment;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.view.Window;
//import android.widget.AdapterView;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.google.gson.Gson;
//import com.google.gson.reflect.TypeToken;
//import com.lidroid.xutils.HttpUtils;
//import com.lidroid.xutils.exception.HttpException;
//import com.lidroid.xutils.http.HttpHandler;
//import com.lidroid.xutils.http.RequestParams;
//import com.lidroid.xutils.http.ResponseInfo;
//import com.lidroid.xutils.http.callback.RequestCallBack;
//import com.lidroid.xutils.http.client.HttpRequest;
//import com.lidroid.xutils.util.PreferencesCookieStore;
//import com.btex.app.Config;
//import com.btex.app.R;
//import com.btex.app.TradeActivity;
//import com.btex.app.adapter.CoinsAdapter;
//import com.btex.app.entity.BannerInfo;
//import com.btex.app.entity.CoinInfo;
//import com.btex.app.myView.XListView;
//import com.btex.app.tools.CookieTool;
//import com.btex.app.tools.LogUtil;
//import com.btex.app.tools.Tools;
//
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import java.io.File;
//import java.lang.reflect.Type;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * Created by wcx on 16/3/16.
// */
//public class FragmentHangQings extends BaseFragment implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
//    private static final String TAG = "FragmentHangQing";
//    private XListView mListView;
//    private CoinsAdapter coinsAdapter;
//    List<BannerInfo> bannerInfos;
//    private CountDownTimer countDownTimer = null;
//    private List<CoinInfo> coinInfos = new ArrayList<>();
//    private PreferencesCookieStore cookieStore;
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        cxt = getActivity();
//        view = inflater.inflate(R.layout.activity_list_refresh_nobar, null);
//
//        View headerView = inflater.inflate(R.layout.fragment_header_hangqing, null);
//        coinsAdapter = new CoinsAdapter(cxt);
//        cookieStore = CookieTool.getCookieStore(cxt, sp);
//        setXListView(headerView);
////        setBanner();  //轮播大图
//        setData();
////        getVerSion();
//
//
//        //定时刷新消息
//        countDownTimer = new CountDownTimer(86400000, 20000) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//                Toast.makeText(cxt, "数据刷新了", Toast.LENGTH_SHORT).show();
//                loadCoinsInfo();
//            }
//
//            @Override
//            public void onFinish() {
//            }
//        };
//
//        return view;
//    }
//
//
//    @Override
//    public void setData() {
//        loadCoinsInfo();
//    }
//
//
//    private void loadCoinsInfo() {
//        RequestParams params = new RequestParams();
//        params.addQueryStringParameter("num_index", "1");
//        HttpHandler<String> httpUtils = new HttpUtils().send(HttpRequest.HttpMethod.GET, Config.URL_COIN_INFO, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                String result = responseInfo.result;
//                LogUtil.i(TAG, "result:  " + result + "   url: " + Config.URL_COIN_INFO);
//                try {
//                    JSONObject obj = new JSONObject(result);
//                    if ("1".equals(obj.getString(Config.KEY_CODE))) {
//                        Gson gson = new Gson();
//
//                        Type type = new TypeToken<ArrayList<CoinInfo>>() {
//                        }.getType();
//                        coinInfos = gson.fromJson(obj.getString(Config.KEY_DATA), type);
//                        if (coinInfos != null) {
//                            coinsAdapter.addData(true, coinInfos);
//                        }
//                    }
//                } catch (Exception e) {
//                    LogUtil.i(TAG, e.toString());
//                }
//                mListView.stopRefresh();
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//                LogUtil.i(TAG, msg);
//                mListView.stopRefresh();
//            }
//        });
//    }
//
//    @Override
//    public void onResume() {
//        super.onResume();
//        countDownTimer.start();
//    }
//
//
//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//    }
//
//    @Override
//    public void onRefresh() {
//        loadCoinsInfo();
////        getVerSion();
//
////        getBannerInfo();
//    }
//
//    @Override
//    public void onLoadMore() {
//
//    }
//
//
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        if (position > 1) {
//            CoinInfo coinInfo = (CoinInfo) coinsAdapter.getItem(position - 2);
//            Intent i = new Intent(cxt, TradeActivity.class);
//            i.putExtra("coinInfo", coinInfo);
//            startActivity(i);
//        }
//    }
//
//
//    /**
//     * 轮播大图
//     */
////    private void setBanner() {
////        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
////        llPoints = (LinearLayout) view.findViewById(R.id.ll_points);
////        mViewPager.setOnTouchListener(this);
////        initAutoChange();
////        //轮播大图
////        mViewPager.setAdapter(new MyPagerAdapter(bannerInfos, cxt));
////        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
////            @Override
////            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
////
////            }
////
////            @Override
////            public void onPageSelected(int position) {
////                // 刷新下边的描述信息和切换点的状态
////                // 取余之后的position
////                if (bannerInfos != null) {
////                    int newPosition = position % bannerInfos.size();
////                    llPoints.getChildAt(previousPosition).setEnabled(false);
////                    llPoints.getChildAt(newPosition).setEnabled(true);
////                    previousPosition = newPosition;
////                }
////            }
////
////            @Override
////            public void onPageScrollStateChanged(int state) {
////
////            }
////        });
////
////    }
//    private void getBannerInfo() {
//
//        //"index_banner");//获取并设置轮转大图
//        //getBannerInfo("index_free_feeds_top  广告位
//        RequestParams params = new RequestParams();
//
//        params.addQueryStringParameter("position", "index_banner");
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.URL_ADVERT_ONE, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                String result = responseInfo.result.toString();
//                try {
//                    JSONObject jsonObj = new JSONObject(result);
//                    if ("1".equals(jsonObj.getString("code"))) {
//                        Gson gson = new Gson();
//                        Type listType = new TypeToken<ArrayList<BannerInfo>>() {
//                        }.getType();
//                        bannerInfos = gson.fromJson(jsonObj.getString("data"), listType);
//                        if (bannerInfos != null) {
//                            if (bannerInfos.size() == 1) {
//
//                            }
////                            initViewPagerData();
//                        }
//
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//        });
//    }
//
//
//    /**
//     * 初始化Listview
//     *
//     * @param headerView
//     */
//    private void setXListView(View headerView) {
//        mListView = (XListView) view.findViewById(R.id.list);
//        mListView.setXListViewListener(this);
//        mListView.setOnItemClickListener(this);
//        mListView.setPullLoadEnable(false);
//        mListView.setPullRefreshEnable(true);
////        mListView.addHeaderView(headerView);
//        mListView.setAdapter(coinsAdapter);
//    }
//
////    private void initViewPagerData() {
////        // 填充数据
////        View view;
////        LayoutParams lp;
////
////        if (bannerInfos != null && bannerInfos.size() != 1) {
////            for (int i = 0; i < bannerInfos.size(); i++) {
////                view = new View(cxt);
////                int width_heigt = DensityUtil.dip2px(cxt, 7);
////                ;
////                lp = new LayoutParams(width_heigt, width_heigt);
////                lp.leftMargin = DensityUtil.dip2px(cxt, 5);
////                view.setLayoutParams(lp);
////                view.setBackgroundResource(R.drawable.point_background);
////                view.setEnabled(false);
////                llPoints.addView(view);
////            }
////        }
////        mViewPager.post(new Runnable() {
////            @Override
////            public void run() {
////                mViewPager.setAdapter(new MyPagerAdapter(bannerInfos, cxt));
////            }
////        });
////        if (bannerInfos.size() > 1) {
////            // 得到余数
////            int m = (Integer.MAX_VALUE / 2) % bannerInfos.size();  //0,1,2,3,4
////            int item = (Integer.MAX_VALUE / 2) - m;
////            mViewPager.setCurrentItem(item);
////            llPoints.getChildAt(previousPosition).setEnabled(true);
////        }
////    }
//
//
//    @Override
//    public void onPause() {
//        super.onPause();
//
//    }
//
//    @Override
//    public void onStop() {
//        super.onStop();
//
//    }
//
//    @Override
//    public void onDestroy() {
//
//        super.onDestroy();
//    }
//
//    /**
//     * 获取版本hao
//     */
//    private void getVerSion() {
//        LogUtil.i(TAG, "xxxxxxxx");
//
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.URL_VERSION, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                String result = responseInfo.result.toString();
//                LogUtil.i(TAG, "version_result:" + result + "   cookies:" + cookieStore.getCookies().toString());
//                if (cookieStore.getCookie("SESSID") != null) {
//                    String token = cookieStore.getCookie("SESSID").getValue();
//                    LogUtil.i(TAG, "....token:" + token);
//                    sp.putString(Config.TOKEN, token);
//                }
//                try {
//                    JSONObject jsonObj = new JSONObject(result);
//                    if ("1".equals(jsonObj.getString(Config.KEY_CODE))) {
//                        jsonObj = jsonObj.getJSONObject(Config.KEY_DATA);
//                        String version = jsonObj.getJSONObject("android").getString("version");
//                        String url = jsonObj.getJSONObject("android").getString("new_pakage");
//                        String myversion = Tools.getVersion(cxt);
//                        Double v1 = Double.parseDouble(myversion);
//                        Double v2 = Double.parseDouble(version);
//                        if (v2 > v1) {
////                            PrompManager.getPrompManager(cxt).showUpdateDialog(TabsActivity.this,url);
//                            showToLoadDialog(url);
//                        }
//                        LogUtil.i(TAG, "version:" + version + "  myversion:" + myversion);
//
//                    }
//                } catch (JSONException e) {
//                    LogUtil.i(TAG, e.toString());
//                }
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//        });
//    }
//
//    private void showToLoadDialog(final String url) {
//        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_version_update, null);
//        final Dialog dialog = new Dialog(getActivity());
//        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        dialog.setCancelable(false);// 是否用“返回键”取消
//        dialog.setCanceledOnTouchOutside(false);
//        dialog.setContentView(view);
//        TextView tv_content = (TextView) view.findViewById(R.id.tv_dialog_content);
//        TextView tv_confirm = (TextView) view.findViewById(R.id.tv_confirm);
//        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
//        tv_content.setText("您好，发现新的版本，请您升级，否则将会影响到您的使用");
//        tv_confirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View arg0) {
//                dialog.dismiss();
//                toDownload(url);
//            }
//        });
//        tv_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View arg0) {
//                dialog.dismiss();
//                getActivity().finish();
//            }
//        });
//        // 弹出
//        dialog.show();
//    }
//
//    private void toDownload(String url) {
//        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//            final HttpHandler handler = new HttpUtils().download(url, "/sdcard/com.toubi.apk", false, false, new RequestCallBack<File>() {
//                @Override
//                public void onSuccess(ResponseInfo<File> responseInfo) {
////                            tvProgress.setText(cxt.getString(R.string.download_ovew));
//                    installApk(responseInfo.result.getPath());
//                }
//
//                @Override
//                public void onLoading(long total, long current, boolean isUploading) {
//                    super.onLoading(total, current, isUploading);
//                    int progress = (int) (current * 100 / total);
////                            tvProgress.setText("正在下载:" + progress + "%");
//                }
//
//                @Override
//                public void onFailure(HttpException error, String msg) {
//                    Toast.makeText(cxt, cxt.getString(R.string.update_fail), Toast.LENGTH_SHORT).show();
//                }
//
//                /**
//                 * 安装一个apk文件，调用系统安装应用PackageInstaller
//                 * @param path 要安装的apk路径
//                 */
//                private void installApk(String path) {
//                    File file = new File(path);
//                    Intent intent = new Intent();
//                    intent.setAction("android.intent.action.VIEW");
//                    intent.addCategory("android.intent.category.DEFAULT");
//                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
//                    cxt.startActivity(intent);
//                }
//
//            });
//
//
//        } else {
//            Toast.makeText(cxt, "sd卡不可用，更新失败", Toast.LENGTH_SHORT).show();
//            getActivity().finish();
//            return;
//        }
//    }
//
//
//}
